<?php
class Merchant_WarehouseProductsController extends Zend_Controller_Action
{
	
	public function indexAction(){
        //if($this->_request->isPost()){
            $page = $this->_request->getParam('page',1);
    	    $pageSize = $this->_request->getParam('pageSize',20);
            $product_sku = $this->_request->getParam('product_sku','');
             
            $product_title = $this->_request->getParam('product_title','');
            $product_status = $this->_request->getParam('product_status','');
            $serviceObj = Merchant_Service_Product::getInstance();
			$loginInfo = new Zend_Session_Namespace("customer");
             //var_dump($loginInfo->customer["customer_id"]);
            $customer_id = $loginInfo->customer["customer_id"];           
           
            $condition = array(
                'productStatus'       =>1,
				'productSku'   	   =>$product_sku,
				'product_title'		   =>$product_title,
                'customer_id'          =>$customer_id,
                'sortColumn'=>'sku',
                'sortMethod'=>'asc'
            	
            );
 
            $rows = $serviceObj->getList($condition,$page,$pageSize,$customer_id); 
            $count = $serviceObj->getCount($condition,$customer_id);
 			foreach ($rows as $key => $row){
 				$rows[$key]['product_status'] = $serviceObj->getStatusText($row['product_status']);
				$rows[$key]['category'] = $serviceObj->getProductCategory($row['product_id']); 				
 			}
            $json = array(
                'ask'    => true,
                'list'   => array(
                    'list' =>$rows,
                    'count' =>$count,
                ),
            );
      
        echo Zend_Json::encode($json);
        exit;
    }

    public function getProductInfoAction(){
        if($this->_request->isPost()){
            $product_id = $this->_request->getParam('product_id',1);
    	    $pageSize = $this->_request->getParam('pageSize',20);
            $product_sku = $this->_request->getParam('product_sku','');
            $warehouse_id = $this->_request->getParam('warehouse_id','');
            $product_title = $this->_request->getParam('product_title','');
            $product_status = $this->_request->getParam('product_status','');
            $serviceObj = Merchant_Service_Product::getInstance();
            $loginInfo = $serviceObj->getLoginInfo();
            $customer_id = $loginInfo->customer["customer_id"];
            $condition = array(
                'product_sku'       =>$product_sku,
                'product_title'     =>$product_title,
                'product_status'    =>$product_status,
                'customer_id'       =>$customer_id,
                'warehouse_id'       =>$warehouse_id,
            );

            $list = Merchant_Service_WarehouseProducts::getByConditionJoin($condition,'*','product_sku',$page,$pageSize);
            $list = Merchant_Service_WarehouseProducts::getInfo($list);
            $count = Merchant_Service_WarehouseProducts::getByConditionJoin($condition,'count(*)');
            $json = array(
                'ask'    => true,
                'list'   => array(
                    'list' =>$list,
                    'count' =>$count,
                ),
            );

        }else{
            $json = array(
                'ask'    => false,
                'list'   => null,
            );
        }
        echo Zend_Json::encode($json);
        exit;
    }
	
		public function viewInventoryAction(){
		$serviceObj = new Merchant_Service_Product();
		$loginInfo = $serviceObj->getLoginInfo();
		$this->view->customerId = $loginInfo->customer["customer_id"];
		$this->view->status = $serviceObj->statusText;
		
		$condition = array();
		$condition['customer_id'] = $loginInfo->customer["customer_id"];
		
		$total = Merchant_Service_WarehouseProducts::getByConditionJoin($condition,'count(*)',"",0,0);
		$this->view->total = $total;
		echo Cff::renderTpl("merchant/warehouse/inventory.tpl","user-layout");
	}
	
	public function inventoryListAction(){
		$page = $this->_request->getParam("page",1);
		$pageSize = $this->_request->getParam("pageSize",20);
		$page = $page?$page:1;
		$productSku = $this->_request->product_sku;
		$serviceObj = new Merchant_Service_Product();
		$loginInfo = $serviceObj->getLoginInfo();
		$this->view->customerId = $loginInfo->customer["customer_id"];
		$this->view->status = $serviceObj->statusText;
		
		$condition = array();
		$condition['customer_id'] = $loginInfo->customer["customer_id"];
		$condition['product_sku'] = $productSku;
		//$total = Merchant_Service_WarehouseProducts::getByConditionJoin($condition,'count(*)',"",0,0);
		$total = Merchant_Service_ProductStocket::getByConditionJoin($condition,'count(*)',"",0,0);
		
		$return = array("ask" => 0);
		if($total){
			$inventoryArr = Merchant_Service_ProductStocket::getByConditionJoin($condition,'*',"",$page,$pageSize);
			$return = array(
				"ask" => true,
				"result" => $inventoryArr,
				"total" => $total,
			);
		}
		
		die(json_encode($return));	
	}

	public function viewInventoryInfoAction(){
		$id = $this->_request->productId;
		$serviceObj = new Merchant_Service_Product();
		$loginInfo = $serviceObj->getLoginInfo();
		$wps = Merchant_Service_WarehouseProducts::getByProductId($id,$loginInfo->customer["customer_id"]);
		if($wps){
			$this->view->wps = $wps;
		}
		$this->view->productId = $id;
		echo $this->view->render("merchant/warehouse/inventory-info.tpl");
	}
	
	public function problemAction(){
		//if($this->_request->isPost()){
		$page = $this->_request->getParam('page',1);
		$page = $page<=0?1:$page;
		$pageSize = $this->_request->getParam('pageSize',20);
		$product_sku = $this->_request->getParam('product_sku','');
		 
		$product_title = $this->_request->getParam('product_title','');
		$product_status = $this->_request->getParam('product_status','');
		$serviceObj = Merchant_Service_Product::getInstance();
		$loginInfo = new Zend_Session_Namespace("customer");
		//var_dump($loginInfo->customer["customer_id"]);
		$customer_id = $loginInfo->customer["customer_id"];
		$quantity_problem = $this->_request->getParam("quantity_problem","");
	
	
		
		$condition = array(
				'product_status'       =>1,
				'product_sku'   	   =>$product_sku,
				'product_title'		   =>$product_title,
				'customer_id'          =>$customer_id,
				'quantity_problem'     =>$quantity_problem
		);
		
		$count = Merchant_Service_ProductStocket::getByConditionJoin($condition,"count(*)");
		$rows = array();
		if($count>0){
			
			$rows = Merchant_Service_ProductStocket::getByConditionJoin($condition,"*","",$page,$pageSize);
// 					print_r($rows);
// 					echo $count;exit;
			foreach ($rows as $key => $row){
				$rows[$key]['product_status'] = $serviceObj->getStatusText($row['product_status']);
				$rows[$key]['category'] = $serviceObj->getProductCategory($row['product_id']);
			}
		}
		$json = array(
				'ask'    => true,
				'list'   => array(
						'list' =>$rows,
						'count' =>$count,
				),
		);
	
		echo Zend_Json::encode($json);
		exit;
	}

	
}